package com.tengfei.api.course;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.course.domain.bo.VideoBo;
import com.tengfei.main.course.domain.vo.VideoVo;
import com.tengfei.main.course.service.IVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 视频Controller
 *
 * @author tengfei
 * @date 2022-07-26
 */
@Validated
@Api(value = "视频控制器", tags = {"视频管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/course/video")
public class VideoController extends BaseController {

    private final IVideoService iVideoService;

    @ApiOperation("上传视频文件")
    @SaCheckPermission("course:video:add")
    @Log(title = "上传视频文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public R<VideoVo> uploadVideo(@RequestPart("video") MultipartFile file) {
        VideoVo video = iVideoService.uploadVideo(file);
        return R.ok(video);
    }


    /**
     * 查询视频列表
     */
    @ApiOperation("查询视频列表")
    @SaCheckPermission("course:video:list")
    @GetMapping("/list")
    public TableDataInfo<VideoVo> list(VideoBo bo, PageQuery pageQuery) {
        return iVideoService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取视频时长
     */
    @ApiOperation("获取视频时长")
    @SaCheckPermission("course:video:query")
    @GetMapping("/duration/{videoId}")
    public R<Integer> getDuration(@ApiParam("视频id")
                                  @NotNull(message = "视频id不能为空")
                                  @PathVariable("videoId") Long videoId) {
        return R.ok(iVideoService.queryDurationById(videoId));
    }

    /**
     * 获取视频详细信息
     */
    @ApiOperation("获取视频详细信息")
    @SaCheckPermission("course:video:query")
    @GetMapping("/{videoId}")
    public R<VideoVo> getInfo(@ApiParam("视频id")
                              @NotNull(message = "视频id不能为空")
                              @PathVariable("videoId") Long videoId) {
        return R.ok(iVideoService.queryById(videoId));
    }

    /**
     * 修改视频
     */
    @ApiOperation("修改视频")
    @SaCheckPermission("course:video:edit")
    @Log(title = "修改视频", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VideoBo bo) {
        return toAjax(iVideoService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除视频
     */
    @ApiOperation("批量删除视频")
    @SaCheckPermission("course:video:remove")
    @Log(title = "批量删除视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("视频id数组")
                          @NotEmpty(message = "视频id数组不能为空")
                          @RequestParam("videoIds[]") List<Long> ids) {
        return toAjax(iVideoService.deleteWithValidByIds(ids) ? 1 : 0);
    }
}
